package com.example.springboot.controller;

import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 功能：与前端交互的窗口 前端调用的接口
 * 作者：陈雷
 * 日期：2024/10/18 17:31
 */

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    //    新增用户信息
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //    修改用户信息
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        try {
            userService.updateUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("修改数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //    删除用户信息
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        try {
            userService.deleteUser(id);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("删除数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //    批量删除用户信息
    @DeleteMapping("/delete/batch")
    public Result batchdeleteUser(@RequestBody List<Integer> ids) {
        try {
            userService.batchdeleteUser(ids);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("批量删除数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
@AuthAccess
    //    查询所有用户信息
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> users;
        try {
            users = userService.selectAll();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("查询数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success(users);
    }

    //    按id查询用户信息
    @GetMapping("/selectByid/{id}")
    public Result selectByid(@PathVariable Integer id) {
        User users;
        try {
            users = userService.selectByid(id);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("查询数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success(users);
    }

    //用户封禁
    @PutMapping("/banned")
    public Result banned(@RequestBody User user) {
      String username = user.getUsername();
      userService.banned(username);
      return Result.success();
    }

    //用户封禁
    @PutMapping("/unbanned")
    public Result unbanned(@RequestBody User user) {
        String username = user.getUsername();
        userService.unbanned(username);
        return Result.success();
    }


}